package com.ibitcube.http.api;

import com.ibitcube.http.api.util.HttpUtil;
import com.ibitcube.http.api.util.RandomUtil;
import com.ibitcube.http.constants.MsgT;

import java.util.Map;
import java.util.HashMap;
import com.ibitcube.http.api.util.ParamValidate;
import com.alibaba.fastjson.JSON;

/**
 * 比特魔方支付接口
 */
public class Pay 
{
	/**
	 * 正式交易环境地址
	 */
	public static final String apiUrl = "https://api.pay.ibitcube.com/webwt/h5pay/gateway.do";
	
	/**
	 * 测试交易环境地址
	 */
	public static final String apiUrlTest = "http://api.test.pay.ibitcube.com/webwt/h5pay/gateway.do";
	
	/**
	 * 本地测试环境地址9098
	 */
	public static final String apiUrlTestLocal_9098 = "http://localhost:9098/webwt/h5pay/gateway.do";
	
	/**
	 * 本地测试环境地址8280
	 */
	public static final String apiUrlTestLocal_8280 = "http://localhost:8280/webwt/h5pay/gateway.do";
	
	/**
	 * 商户入驻接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String merchantBind(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "service_phone", "category_id", "id_card_num", 
				"id_card_hand_img_url", "store_front_img_url", "bank_card_no", "name", "alias_name",
				"store_address", "id_card_name", "card_holder", "sign", "province", "city", "district");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_MERCHANT_BIND);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		
		data.put("nonceStr", RandomUtil.generateString(16));

		data.put("service_phone", String.valueOf(params.get("service_phone")));
		data.put("category_id", String.valueOf(params.get("category_id")));
		data.put("id_card_num", String.valueOf(params.get("id_card_num")));
		data.put("id_card_hand_img_url", String.valueOf(params.get("id_card_hand_img_url")));
		data.put("store_front_img_url", String.valueOf(params.get("store_front_img_url")));
		
		data.put("name",  String.valueOf(params.get("name")));
		data.put("alias_name",  String.valueOf(params.get("alias_name")));
		data.put("store_address",  String.valueOf(params.get("store_address")));
		data.put("province",  String.valueOf(params.get("province")));
		data.put("city",  String.valueOf(params.get("city")));
		data.put("district",  String.valueOf(params.get("district")));
		data.put("id_card_name",  String.valueOf(params.get("id_card_name")));
		
		data.put("bank_card_no", String.valueOf(params.get("bank_card_no")));
		data.put("card_holder",  String.valueOf(params.get("card_holder")));
		
		// 对公银行账户处理
		if(params.containsKey("is_public_account")  && String.valueOf(params.get("is_public_account")).trim().equals("1")) {
			data.put("is_public_account", 1);
			data.put("open_bank", String.valueOf(params.get("open_bank")));
			data.put("id_type", String.valueOf(params.get("id_type")));
			data.put("id_code", String.valueOf(params.get("id_code")));
			data.put("bank_type", String.valueOf(params.get("bank_type")));
			data.put("bank_code", String.valueOf(params.get("bank_code")));
			data.put("s_bank_code", String.valueOf(params.get("s_bank_code")));
			data.put("mobile_phone", String.valueOf(params.get("mobile_phone")));
		}
		
		// 联系人名字
		if(params.containsKey("contact_name")) {
			data.put("contact_name", String.valueOf(params.get("contact_name")));
		}
		// 联系人电话
		if(params.containsKey("contact_phone")) {
			data.put("contact_phone", String.valueOf(params.get("contact_phone")));
		}
		// 联系人手机号
		if(params.containsKey("contact_mobile")) {
			data.put("contact_mobile", String.valueOf(params.get("contact_mobile")));
		}
		// 联系人邮箱
		if(params.containsKey("contact_email")) {
			data.put("contact_email", String.valueOf(params.get("contact_email")));
		}
		
		// 银行卡号（需保证同持卡人同身份证姓名一致）；若上传了card_no，business_license，business_license_type，type，其中一个参数，则四个参数必传
		if(params.containsKey("card_no")) {
			data.put("card_no", String.valueOf(params.get("card_no")));
		}
		// 营业执照编号（企业或者个体工商户提供营业执照，事业单位提供事证号）；若上传了card_no，business_license，business_license_type，type，其中一个参数，则四个参数必传
		if(params.containsKey("business_license")) {
			data.put("business_license", String.valueOf(params.get("business_license")));
		}
		// 商户证件类型，取值范围：NATIONAL_LEGAL：(营业执照)；NATIONAL_LEGAL_MERCE：(营业执照(多证合一)；INST_RGST_CTF：(事业单位法人证书)；若上传了card_no，business_license，business_license_type，type，其中一个参数，则四个参数必传
		if(params.containsKey("business_license_type")) {
			data.put("business_license_type", String.valueOf(params.get("business_license_type")));
		}
		// 商户联系人类型，取值范围：LEGAL_PERSON：(法人)；CONTROLLER：(实际控制人)；AGENT：(代理人)；OTHER：(其他)；若上传了card_no，business_license，business_license_type，type，其中一个参数，则四个参数必传
		if(params.containsKey("contact_type")) {
			data.put("contact_type", String.valueOf(params.get("contact_type")));
		}
		
		// 商户汇率可选参数
		if(params.containsKey("merchant_rate")) {
			data.put("merchant_rate", String.valueOf(params.get("merchant_rate")));
		}
		
		// 商户备注可选参数
		if(params.containsKey("memo")) {
			data.put("memo", String.valueOf(params.get("memo")));
		}
		
		// 业务员编号可选参数
		if(params.containsKey("salesId")) {
			data.put("salesId", String.valueOf(params.get("salesId")));
		}
		
		// 微信ID可选参数
		if(params.containsKey("wechatId")) {
			data.put("wechatId", String.valueOf(params.get("wechatId")));
		}
		
		// 烟草入驻可选参数
		if(params.containsKey("HsLicense")) {
			data.put("HsLicense", String.valueOf(params.get("HsLicense")));
		}
		if(params.containsKey("HsMchCode")) {
			data.put("HsMchCode", String.valueOf(params.get("HsMchCode")));
		}
		if(params.containsKey("HsA")) {
			data.put("HsA", String.valueOf(params.get("HsA")));
		}
		if(params.containsKey("HsB")) {
			data.put("HsB", String.valueOf(params.get("HsB")));
		}
		if(params.containsKey("HsC")) {
			data.put("HsC", String.valueOf(params.get("HsC")));
		}
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 商户信息查询接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String merchantQuery(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_MERCHANT_QUERY);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		
		data.put("nonceStr", RandomUtil.generateString(16));
		
		
		// 商户入驻可选参数
		if(params.containsKey("bindId")) {
			data.put("bindId", String.valueOf(params.get("bindId")));
		}
		
		// 微信ID可选参数
		if(params.containsKey("wechatId")) {
			data.put("wechatId", String.valueOf(params.get("wechatId")));
		}
		// 烟草商户代码可选参数
		if(params.containsKey("HsMchCode")) {
			data.put("HsMchCode", String.valueOf(params.get("HsMchCode")));
		}
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 支付宝H5预创建接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String alipayTradeCreate(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign", "goodsName", "orderAmt",
				"orderId", "bindId", "goodsName");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_ALIPAY_TRADE_CREATE);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		
		data.put("nonceStr", RandomUtil.generateString(16));
		data.put("orderId", String.valueOf(params.get("orderId")));
		data.put("orderAmt", String.valueOf(params.get("orderAmt")));
		data.put("bindId", String.valueOf(params.get("bindId")));
		data.put("goodsName", String.valueOf(params.get("goodsName")));
		
		// 商品的描述（可选）
		if(params.containsKey("body")) {
			data.put("body", String.valueOf(params.get("body")));
		}
		
		// 可选参数
		if(params.containsKey("discountable_amount")) {
			data.put("discountable_amount", String.valueOf(params.get("discountable_amount")));
		}
		
		// 可选参数
		if(params.containsKey("undiscountable_amount")) {
			data.put("undiscountable_amount", String.valueOf(params.get("undiscountable_amount")));
		}
		// 可选参数
		if(params.containsKey("buyer_logon_id")) {
			data.put("buyer_logon_id", String.valueOf(params.get("buyer_logon_id")));
		}
		// 可选参数
		if(params.containsKey("buyer_id")) {
			data.put("buyer_id", String.valueOf(params.get("buyer_id")));
		}
		// 可选参数
		if(params.containsKey("callback_url")) {
			data.put("callback_url", String.valueOf(params.get("callback_url")));
		}
		// 可选参数
		if(params.containsKey("notifyUrl")) {
			data.put("notifyUrl", String.valueOf(params.get("notifyUrl")));
		}
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 支付宝扫码接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String alipayTradePreCreate(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign", "orderAmt",
				"orderId", "bindId", "goodsName");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_ALIPAY_TRADE_PRE_CREATE);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		
		data.put("nonceStr", RandomUtil.generateString(16));
		data.put("orderId", String.valueOf(params.get("orderId")));
		data.put("orderAmt", String.valueOf(params.get("orderAmt")));
		data.put("bindId", String.valueOf(params.get("bindId")));
		data.put("goodsName", String.valueOf(params.get("goodsName")));
		
		// 商品的描述（可选）
		if(params.containsKey("body")) {
			data.put("body", String.valueOf(params.get("body")));
		}
		
		// 可选参数
		if(params.containsKey("discountable_amount")) {
			data.put("discountable_amount", String.valueOf(params.get("discountable_amount")));
		}
		
		// 可选参数
		if(params.containsKey("undiscountable_amount")) {
			data.put("undiscountable_amount", String.valueOf(params.get("undiscountable_amount")));
		}
		
		// 可选参数
		if(params.containsKey("notifyUrl")) {
			data.put("notifyUrl", String.valueOf(params.get("notifyUrl")));
		}
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 微信公众号支付接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String wxTradeCreate(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign", "orderAmt","sub_openid",
				"orderId", "bindId", "body");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_WX_TRADE_CREATE);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		data.put("nonceStr", RandomUtil.generateString(16));
		
		
		data.put("orderId", String.valueOf(params.get("orderId")));
		data.put("sub_openid", String.valueOf(params.get("sub_openid")));
		data.put("orderAmt", String.valueOf(params.get("orderAmt")));
		data.put("bindId", String.valueOf(params.get("bindId")));
		data.put("body", String.valueOf(params.get("body")));
		
		// 可选参数spBillCreateIp
		if(params.containsKey("spbill_create_ip")) {
			data.put("spbill_create_ip", String.valueOf(params.get("spbill_create_ip")));
		}
		// 可选参数notifyUrl
		if(params.containsKey("notifyUrl")) {
			data.put("notifyUrl", String.valueOf(params.get("notifyUrl")));
		}
		// 额外的公众号appid来满足服务商的额外的业务需求
		if(params.containsKey("sub_appid")) {
			data.put("sub_appid", String.valueOf(params.get("sub_appid")));
		}
		
		// 微信子商户号
		if(params.containsKey("sub_mch_id")) {
			data.put("sub_mch_id", String.valueOf(params.get("sub_mch_id")));
		}
		
		
		// 商品标记，代金券或立减优惠功能的参数， 说明详见代金券或立减优惠
		if(params.containsKey("goods_tag")) {
			data.put("goods_tag", String.valueOf(params.get("goods_tag")));
		}
		
		// 支付成功后的跳转地址
		if(params.containsKey("callback_url")) {
			data.put("callback_url", String.valueOf(params.get("callback_url")));
		}
		
		// 支付过程中用户取消支付或支付失败跳转页面
		if(params.containsKey("cancel_url")) {
			data.put("cancel_url", String.valueOf(params.get("cancel_url")));
		}
		
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 微信扫码支付接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String wxTradePreCreate(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign", "orderAmt",
				"orderId", "bindId", "body");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_WX_TRADE_PRE_CREATE);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		data.put("nonceStr", RandomUtil.generateString(16));

		data.put("orderId", String.valueOf(params.get("orderId")));
		data.put("orderAmt", String.valueOf(params.get("orderAmt")));
		data.put("bindId", String.valueOf(params.get("bindId")));
		data.put("body", String.valueOf(params.get("body")));
		
		// 可选参数spBillCreateIp
		if(params.containsKey("spbill_create_ip")) {
			data.put("spbill_create_ip", String.valueOf(params.get("spbill_create_ip")));
		}
		// 可选参数notifyUrl
		if(params.containsKey("notifyUrl")) {
			data.put("notifyUrl", String.valueOf(params.get("notifyUrl")));
		}
		// 可选参数sub_appid
		if(params.containsKey("sub_appid")) {
			data.put("sub_appid", String.valueOf(params.get("sub_appid")));
		}
		// 可选参数goods_tag
		if(params.containsKey("goods_tag")) {
			data.put("goods_tag", String.valueOf(params.get("goods_tag")));
		}
			
		// 微信子商户号
		if(params.containsKey("sub_mch_id")) {
			data.put("sub_mch_id", String.valueOf(params.get("sub_mch_id")));
		}
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 微信刷卡支付接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String wxTradePay(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign", "authCode", "orderAmt",
				"orderId", "bindId", "body");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_WX_TRADE_PAY);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		data.put("nonceStr", RandomUtil.generateString(16));
		data.put("orderId", String.valueOf(params.get("orderId")));
		data.put("authCode", String.valueOf(params.get("authCode")));
		data.put("orderAmt", String.valueOf(params.get("orderAmt")));
		data.put("bindId", String.valueOf(params.get("bindId")));
		data.put("body", String.valueOf(params.get("body")));
		
		// 可选参数spBillCreateIp
		if(params.containsKey("spBillCreateIp")) {
			data.put("spBillCreateIp", String.valueOf(params.get("spBillCreateIp")));
		}
		
		// 可选参数sub_appid
		if(params.containsKey("sub_appid")) {
			data.put("sub_appid", String.valueOf(params.get("sub_appid")));
		}
		// 可选参数goods_tag
		if(params.containsKey("goods_tag")) {
			data.put("goods_tag", String.valueOf(params.get("goods_tag")));
		}
		// 微信子商户号
		if(params.containsKey("sub_mch_id")) {
			data.put("sub_mch_id", String.valueOf(params.get("sub_mch_id")));
		}
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 支付宝刷卡支付接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String alipayTradePay(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign", "authCode", "orderAmt",
				"orderId", "bindId", "subject");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_ALIPAY_TRADE_PAY);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		data.put("nonceStr", RandomUtil.generateString(16));
		data.put("orderId", String.valueOf(params.get("orderId")));
		data.put("authCode", String.valueOf(params.get("authCode")));
		data.put("orderAmt", String.valueOf(params.get("orderAmt")));
		data.put("bindId", String.valueOf(params.get("bindId")));
		// 订单标题
		data.put("subject", String.valueOf(params.get("subject")));
		
		// 可选参数body
		if(params.containsKey("body")) {
			data.put("body", String.valueOf(params.get("body")));
		}
		// 可选参数notifyUrl
		if(params.containsKey("notifyUrl")) {
			data.put("notifyUrl", String.valueOf(params.get("notifyUrl")));
		}
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 订单查询接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String orderQuery(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign", "tranDate","orderId");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_ORDER_QUERY);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		data.put("nonceStr", RandomUtil.generateString(16));
		data.put("orderId", String.valueOf(params.get("orderId")));
		data.put("tranDate", String.valueOf(params.get("tranDate")));
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 订单关闭接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String orderClose(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign","orderId");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_ORDER_CLOSE);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		data.put("nonceStr", RandomUtil.generateString(16));
		data.put("orderId", String.valueOf(params.get("orderId")));
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 订单撤销接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String orderReverse(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_ORDER_REVERSE);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		data.put("nonceStr", RandomUtil.generateString(16));
		data.put("orderId", String.valueOf(params.get("orderId")));
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 支付退款接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String orderRefund(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_ORDER_REFUND);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		data.put("nonceStr", RandomUtil.generateString(16));
		
		// 可选参数orderId
		if(params.containsKey("orderId")) {
			data.put("orderId", String.valueOf(params.get("orderId")));
		}
		
		// 可选参数transactionId
		if(params.containsKey("transactionId")) {
			data.put("transactionId", String.valueOf(params.get("transactionId")));
		}
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 支付退款结果查询接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String orderRefundQuery(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign", "orderId");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_ORDER_REFUND_QUERY);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		data.put("nonceStr", RandomUtil.generateString(16));
		data.put("orderId", String.valueOf(params.get("orderId")));
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 对公账户验证鉴权金额接口
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String bankVerifyMoney(String url, Map<String, Object> params) throws Exception {

		// 检查必须字段参数
		ParamValidate.doing(params, "agtId", "merId", "sign", "bindId");
		
		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> hdata = new HashMap<String, Object>();
		
		data.put("tranCode", MsgT.TRAN_CODE_BANK_VERIFY_MONEY);
		data.put("agtId", String.valueOf(params.get("agtId")));
		data.put("merId", String.valueOf(params.get("merId")));
		data.put("nonceStr", RandomUtil.generateString(16));
		data.put("bindId", String.valueOf(params.get("bindId")));
		data.put("tran_amount", String.valueOf(params.get("tran_amount")));
		
		hdata.put("sign", HttpUtil.getSign(data, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", data);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
	
	/**
	 * 通用接口调用（用户自定义数据）
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String UnifiedApi(String url, Map<String, Object> params) throws Exception {

		HttpUtil http = new HttpUtil(url, HttpUtil.POST);
		Map<String, Object> hdata = new HashMap<String, Object>();
		hdata.put("sign", HttpUtil.getSign(params, String.valueOf(params.get("sign"))));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REQ_BODY", params);
		map.put("REQ_HEAD", hdata);
		String reqPostString = JSON.toJSONString(map);
		System.out.println("请求数据: " + reqPostString);
		String responseData = null;
		responseData = http.post(reqPostString);
		System.out.println("响应数据:" + responseData);
		return responseData;
	}
    
}
